<?php

namespace app\admin\controller;

use app\admin\controller\Base;
use app\common\library\Tree;
use think\Db;

/**
 * 用户组管理
 * Class GroupController
 * @package app\admin\controller\auth
 */
class Help extends Base
{


    public function index()
    {
        $this->redirect(url('base'));
    }
    public function notify()
    {
       redirect('helplist');
    }
    public function news()
    {
        return view('lists');
    }
    public function helplist()
    {
        $list = DB::name('help_category')
            ->order('sort asc')
            ->page(input("param.page"), input('param.limit'))
            ->select();
        $list = Tree::unlimitForLevel($list);
        $this->assign('help_category',$list);
        return view();
    }

    public function category()
    {
        if ($this->request->isPost()) {
            $post = $this->request->param();
            if (count($post) < 2) {
                $this->success('操作完成');
            }

        } else {
            return view();
        }
    }

    public function addcategory()
    {
        if ($this->request->isPost()) {
            $post = $this->request->param();
            //单个字段修改
            $pid=db::name('help_category')->where('id',$post['pid'])->value('pid');
            if($pid>0){
                $this->error('分类不能超过二级');
            }
            Db::startTrans();
            try {

                Db::name('help_category')->insert($post);
                Db::commit();
                $status = 1;
            } catch (\Exception $e) {
                Db::rollback();
                $status = 0;
            }
            if ($status) {
                $this->success('操作成功!');
            }
            $this->error('操作失败');
        }

    }

    public function getcategory()
    {
        $post = $this->request->param();
        $list = DB::name('help_category')
            ->order('sort asc')
            //->page(input("param.page"), input('param.limit'))
            ->select();
        $data['count']=DB::name('help_category')
            ->count();
        $data['data'] = Tree::unlimitForLevel($list);
        if ($data['data']) {
            $data['msg'] = '获取成功';
        } else {
            $data['msg'] = '获取数据为空';
        }
        $data['code'] = 0;
        return json($data);
    }

    public function treecategory()
    {
        $post = $this->request->param();
        $list = DB::name('help_category')
            ->select();
        $color = config('param.set_color');
        foreach ($list as $key => &$item) {
            $item['spread'] = true;
            $new_color = substr($key, -1);
            $item['title'] = '<div style="color:' . $color[(int)$new_color] . '">' . $item['title'] . '</div>';
        }

        $data['data'] = Tree::unlimitForLayer($list, 0, 'children');
        if ($data['data']) {
            $data['msg'] = '获取成功';
        } else {
            $data['msg'] = '获取数据为空';
        }
        $data['code'] = 0;
        return json($data);
    }

    public function delcategory()
    {
        if ($this->request->isPost()) {
            $post = $this->request->param();
            if (!isset($post['id'])) {
                $this->error('请求参数错误');
            }
            Db::startTrans();
            try {
                Db::name('help_category')->where('id', $post['id'])->delete();
                Db::commit();
                $status = 1;
            } catch (\Exception $e) {
                Db::rollback();
                $status = 0;
            }
            if ($status) {
                $this->success('操作成功!');
            }
            $this->error('操作失败');
        }
    }

    public function editcategory()
    {
        if ($this->request->isPost()) {
            $post = $this->request->param();
            //单个字段修改
            Db::startTrans();
            try {
                Db::name('help_category')
                    ->where(['id' => $post['id']])
                    ->strict(false)
                    ->update($post);
                Db::commit();
                $status = 1;
            } catch (\Exception $e) {
                Db::rollback();
                $status = 0;
            }
            if ($status) {
                $this->success('操作成功!');
            }
            $this->error('操作失败');

        }

    }


    public function getcommentcount()
    {
        $data['data'] = Db::name('helpcomment')
            ->where('help_id', input('help_id'))
            ->count();
        if ($data['data']) {
            $data['msg'] = '获取成功';
        } else {
            $data['msg'] = '获取数据为空';
        }
        $data['code'] = 0;
        return json($data);
    }

    public function help_comment_list()
    {
        $data = array();
        $post = $this->request->param();
        unset($post['page']);
        unset($post['limit']);
        unset($post['keyword']);
        $data['count'] = DB::view('helpcomment', 'id,uid,c_time,info')
            ->view('admin', 'nickname', 'helpcomment.uid=admin.id')
            ->where('help_id', $post['help_id'])
            ->count();
        $data['data'] = DB::view('helpcomment', 'id,uid,c_time,info')
            ->view('admin', 'nickname', 'helpcomment.uid=admin.id')
            ->where('help_id', $post['help_id'])
            ->order('id desc')->page(input("param.page"), input('param.limit'))->select();

        foreach ($data['data'] as &$item) {
            $item['c_time'] = date('Y-m-d  H:i:s', $item['c_time']);
        }
        if ($data['count'] > 0 && $data['data']) {
            $data['msg'] = '获取成功';
        } else {
            $data['msg'] = '获取数据为空';
        }
        $data['code'] = 0;
        return json($data);
    }

    public function addcomment()
    {
        if ($this->request->isPost()) {

            Db::startTrans();
            try {
                $post = $this->request->param();
                unset($post['file']);
                $post['c_time'] = time();
                $post['uid'] = session('uid');
                $post['info'] = $this->request->param('info', '', null);
                Db::name('helpcomment')->strict(false)->insert($post);
                Db::commit();
                $status = 1;
            } catch (\Exception $e) {
                Db::rollback();
                $status = 0;
            }
            if ($status) {
                $this->success('操作成功!');
            }
            $this->error('操作失败');

        }
    }

    public function lists()
    {
        $map = array();
        $data = array();
        $post = $this->request->param();
        if (empty($post['title']) || $post['title'] == '') {
        } else {
            $map[] = ['help.title', 'like', '%' . $post['title'] . '%'];
        }
        if (empty($post['category_id']) || $post['category_id'] == '') {
        } else {
            $map[] = ['help.category_id', '=',$post['category_id']];
        }
        $data['count'] = Db::view('help','id,category_id,title')
            ->view(['help_category'=>'id_category'],['title'=>'id_title'],'help.category_id=id_category.id')
            ->view(['help_category'=>'pid_category'],['title'=>'pid_title'],'id_category.pid=pid_category.id')
            ->where($map)
            ->count();
        $data['data'] = Db::view('help','id,category_id,title')
            ->view(['help_category'=>'id_category'],['title'=>'id_title'],'help.category_id=id_category.id')
            ->view(['help_category'=>'pid_category'],['title'=>'pid_title'],'id_category.pid=pid_category.id')
            ->where($map)
            // ->fetchSql(true)
            ->order('id asc')->page(input("param.page"), input('param.limit'))->select();
        //dump($data['data']);
        if ($data['count'] > 0 && $data['data']) {
            $data['msg'] = '获取成功';
        } else {
            $data['msg'] = '获取数据为空';
        }
        $data['code'] = 0;
        return json($data);
    }


    public function helpadd()
    {
        if ($this->request->isPost()) {
            $post = $this->request->param();
            $validate = new \app\admin\validate\Doc();
            if (!$validate->check($post)) {
                $this->error($validate->getError());
            }

            Db::startTrans();
            try {
                $post['info_id'] = Db::name('infoview')->insertGetId(['info' => $this->request->param('info', '', null)]);
                unset($post['info']);
                unset($post['id']);
                unset($post['editormd-html-code']);
                $post['c_time']=time();
                $post['help_id'] = Db::name('help')->strict(false)->insertGetId($post);
                Db::commit();
                $status = 1;
            } catch (\Exception $e) {
                Db::rollback();
                $status = 0;
            }
            if ($status) {
                $this->success('操作成功!');
            }
            $this->error('操作失败');
        } else {
            $list = DB::name('help_category')
                ->order('sort asc')
                ->page(input("param.page"), input('param.limit'))
                ->select();
            $list = Tree::unlimitForLevel($list);
            $this->assign('help_category',$list);
            $this->assign('help_type', input('help_type'));
            return view();
        }
    }

    public function helpdelete()
    {
        if ($this->request->isPost()) {
            $post = $this->request->param();
            if (!isset($post['id'])) {
                $this->error('请求参数错误');
            }

            Db::startTrans();
            try {
                $info = Db::name('help')->where(['id' => $post['id']])->delete();
                Db::commit();
                $status = 1;
            } catch (\Exception $e) {
                Db::rollback();
                $status = 0;
            }
            if ($status) {
                $this->success('操作成功!');
            }
            $this->error('操作失败');

        }
    }


    public function helpview()
    {
        $post = $this->request->param();
        if ($this->request->isPost()) {
            $validate = new \app\admin\validate\Doc();
            if (!$validate->check($post)) {
                $this->error($validate->getError());
            }
            Db::startTrans();
            try {
                Db::name('infoview')->where(['id' => $post['info_id']])->update(['info' => $this->request->param('info', '', null)]);
                unset($post['info']);
                Db::name('help')->where(['id' => $post['id']])->update($post);
                Db::commit();
                $status = 1;
            } catch (\Exception $e) {
                Db::rollback();
                $status = 0;
            }

            if ($status) {
                $this->success('操作成功!');
            }
            $this->error('操作失败');
        } else {
            $info = Db::name('help')
                ->where('id', input('id'))
                ->find();
            $info['info'] = Db::name('infoview')
                ->where('id', $info['info_id'])
                ->value('info');
            //dump($info);
            $info['c_time']=date('Y-m-d',$info['c_time']);
            $this->assign('info', $info);
            return view();
        }
    }


    public function helpedit()
    {
        if ($this->request->isPost()) {
            $post = $this->request->param();
            $validate = new \app\admin\validate\Doc();
            if (!$validate->check($post)) {
                $this->error($validate->getError());
            }
            Db::startTrans();
            try {
                Db::name('infoview')->where(['id' => $post['info_id']])->update(['info' => $this->request->param('info', '', null)]);
                unset($post['info']);
                $post['c_time']=time();
                unset($post['editormd-html-code']);
                Db::name('help')->where(['id' => $post['id']])->update($post);
                Db::commit();
                $status = 1;
            } catch (\Exception $e) {
                Db::rollback();
                $status = 0;
            }

            if ($status) {
                $this->success('操作成功!');
            }
            $this->error('操作失败');
        } else {
            $list = DB::name('help_category')
                ->order('sort asc')
                ->page(input("param.page"), input('param.limit'))
                ->select();
            $list = Tree::unlimitForLevel($list);
            $this->assign('help_category',$list);
            $info = Db::name('help')
                ->where('id', input('param.id'))
                ->find();
            $info['info'] = Db::name('infoview')
                ->where('id', $info['info_id'])
                ->value('info');
            //dump($info);
            $this->assign('info', $info);
            return view('helpadd');
        }
    }
}